package com.zeny.online.controller;


import com.zeny.online.common.OperatorFriendRequestTypeEnum;
import com.zeny.online.common.ResponseResult;
import com.zeny.online.entity.vo.FriendRequestVO;
import com.zeny.online.entity.vo.MyFriendVO;
import com.zeny.online.service.FriendsRequestService;
import com.zeny.online.service.MyFriendsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/online/friends-request")
@Api(tags = "好友请求")
public class FriendsRequestController {

    @Autowired
    private FriendsRequestService friendsRequestService;

    @Autowired
    private MyFriendsService myFriendsService;

    @ApiOperation(value = "添加好友请求")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "myUserId", value = "myUserId", example = "123456789", required = true),
            @ApiImplicitParam(name = "friendUserId", value = "friendUserId", example = "12345678999", required = true),
    })
    @PostMapping("/addFriendRequest")
    public ResponseResult addFriendRequest(String myUserId, String friendUserId) {
        if (StringUtils.isBlank(myUserId)
                || StringUtils.isBlank(friendUserId)
                || myUserId.equals(friendUserId)) {
            return ResponseResult.fail().message("无法添加");
        }
        friendsRequestService.addFriendRequest(myUserId, friendUserId);
        return ResponseResult.ok();
    }

    @PostMapping("/friendRequestList")
    @ApiOperation(value = "好友请求列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true)
    })
    public ResponseResult friendRequestList(String userId) {
        if (StringUtils.isBlank(userId)) {
            return ResponseResult.fail();
        }
        List<FriendRequestVO> friendRequestVOS = friendsRequestService.getUserListFromFriendRequest(userId);
        return ResponseResult.ok().data(friendRequestVOS);
    }

    @PostMapping("/operFriendRequest")
    @ApiOperation(value = "忽略或通过好友请求")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sendUserId", value = "请求方ID", required = true),
            @ApiImplicitParam(name = "acceptUserId", value = "当前用户ID", required = true),
            @ApiImplicitParam(name = "operType", value = "0:忽略, 1:通过", required = true)
    })
    public ResponseResult operFriendRequest(String sendUserId, String acceptUserId, Integer operType) {
        if (StringUtils.isBlank(sendUserId)
                || StringUtils.isBlank(acceptUserId)
                || operType == null
                || OperatorFriendRequestTypeEnum.getMsgByType(operType) == null) {
            return ResponseResult.fail();
        }
        //处理对应的好友请求
        friendsRequestService.operFriendRequest(sendUserId, acceptUserId, operType);
        //返回好友列表
        List<MyFriendVO> myFriendList = myFriendsService.getMyFriendList(acceptUserId);
        return ResponseResult.ok().data(myFriendList);
    }
}

